﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text.RegularExpressions;

public partial class Manager_UserControl_uc_User : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
    }
    private void InitData()
    {
        ClearTextBox();
        DTP_ACCOUNT objAccount = new DTP_ACCOUNT();
        objAccount.USERNAME = string.Empty;
        objAccount.FIRSTNAME = string.Empty;
        objAccount.LASTNAME = string.Empty;
        objAccount.PHONE = string.Empty;
        objAccount.EMAIL = string.Empty;
        objAccount.MSBOPHAN = 0;
        objAccount.CSID = 0;
        objAccount.ACTIVE = chkActive.Checked;
        DisplayUsersInGrid(objAccount);
    }

    private void ClearTextBox()
    {
        GetAllMSBoPhan(-1);
        GetAllCSGetListByID(-1);
        hdfUserID.Value = "-1";
        txtUserName.Text = string.Empty;
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmail.Text = string.Empty;
        ddlMSBOPHAN.SelectedValue = "0";
        ddlCS.SelectedValue = "0";
        chkActive.Checked = true;
        btnSave.Text = "Thêm mới";
        lblAlerting.Text = string.Empty;
    }
    protected void DisplayUsersInGrid(DTP_ACCOUNT objAccount)
    {
        AccountBO acc = new AccountBO();
        List<PRC_DTP_ACCOUNT_SEARCHResult> lst = new List<PRC_DTP_ACCOUNT_SEARCHResult>();
        lst = acc.AccountGet_Search(objAccount).ToList();
        grdUserList.DataSource = lst;
        if (lst.Count > 0)
        {
            grdUserList.PageIndex = 0;
        }
        grdUserList.DataBind();
    }
    private void GetAllMSBoPhan(int id)
    {
        try
        {
            BoPhanBO bphanbo = new BoPhanBO();
            List<DAL.PRC_DTP_BOPHAN_GETLISTBYIDResult> lst = new List<DAL.PRC_DTP_BOPHAN_GETLISTBYIDResult>();
            lst = bphanbo.BoPhanGetListByID(id).ToList();
            if (lst != null)
            {
                ddlMSBOPHAN.DataSource = lst;
                ddlMSBOPHAN.DataTextField = "TENBOPHAN";
                ddlMSBOPHAN.DataValueField = "ID";
                ddlMSBOPHAN.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "0");
                ddlMSBOPHAN.Items.Insert(0, lstParent);
                ddlMSBOPHAN.SelectedIndex = ddlMSBOPHAN.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }

    private void GetAllCSGetListByID(int id)
    {
        try
        {
            AccountBO acc = new AccountBO();
            List<DAL.PRC_DTP_CS_GETLISTBYIDResult> lst = new List<DAL.PRC_DTP_CS_GETLISTBYIDResult>();
            lst = acc.CSGetListByID(id);
            if (lst != null)
            {
                ddlCS.DataSource = lst;
                ddlCS.DataTextField = "NAME";
                ddlCS.DataValueField = "CODE";
                ddlCS.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "0");
                ddlCS.Items.Insert(0, lstParent);
                ddlCS.SelectedIndex = ddlCS.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }

    protected void grdUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hdfUserID.Value = "-1";
        lblAlerting.Text = string.Empty;
        LoadGrid();
    }
    protected void grdUserList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        lblAlerting.Text = string.Empty;
        btnSave.Text = "Cập nhật";
        grdUserList.EditIndex = e.NewEditIndex;
        hdfUserID.Value = grdUserList.DataKeys[e.NewEditIndex].Value.ToString();
        string strUserName = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingUserName")).Text;
        string strFirstName = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingFirstName")).Text;
        string strLastName = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingLastName")).Text;
        string strPhone = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingPhone")).Text;
        string strEmail = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingEmail")).Text;
        string strMSBoPhan = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingMSBoPhan")).Text;
        string strCSID = ((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingCSID")).Text;

        bool Active = bool.Parse(((Label)grdUserList.Rows[e.NewEditIndex].FindControl("lblListingActive")).Text);

        // Bind len control
        txtUserName.Text = strUserName;
        txtFirstName.Text = strFirstName;
        txtLastName.Text = strLastName;
        txtPhone.Text = strPhone;
        txtEmail.Text = strEmail;
        ddlMSBOPHAN.SelectedValue = strMSBoPhan;
        ddlCS.SelectedValue = strCSID;
        chkActive.Checked = Active;

    }
    protected void btnXoaTrang_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập username!";
            return;
        }
        if (txtFirstName.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập tên nhân viên!";
            return;
        }
        if (txtLastName.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập họ nhân viên!";
            return;
        }
        if (txtEmail.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập email của nhân viên!";
            return;
        }
        else
        {
            if (!CheckEmail(txtEmail.Text.Trim()))
            {
                lblAlerting.Text = "Email của nhân viên bạn nhập không đúng!";
                return;
            }
        }
        if (int.Parse(ddlMSBOPHAN.SelectedValue) <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn nhóm người dùng của nhân viên!";
            return;
        }

        // Thuc hien Insert Update
        DTP_ACCOUNT objAccount = new DTP_ACCOUNT();
        objAccount.USERID = int.Parse(hdfUserID.Value);
        objAccount.USERNAME = txtUserName.Text;
        objAccount.FIRSTNAME = txtFirstName.Text;
        objAccount.LASTNAME = txtLastName.Text;
        objAccount.PHONE = txtPhone.Text;
        objAccount.EMAIL = txtEmail.Text;
        objAccount.MSBOPHAN = int.Parse(ddlMSBOPHAN.SelectedValue);
        objAccount.CSID = int.Parse(ddlCS.SelectedValue);
        objAccount.ACTIVE = chkActive.Checked;

        AccountBO acc = new AccountBO();
        if (int.Parse(hdfUserID.Value) <= 0)
        {
            objAccount.PASSWORD = General.EncryptPassword("DTP@1234");
            hdfUserID.Value = acc.AccountInsert(objAccount).ToString();
            if (int.Parse(hdfUserID.Value) > 0)
            {
                btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Thêm mới User thành công!";
            }
            else
            {
                lblAlerting.Text = "Thêm mới User thất bại, bạn vui lòng thử lại!";
            }
        }
        else
            if (acc.AccountUpdate(objAccount))
            {
                //btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Cập nhật User thành công!";
            }
            else
            {
                lblAlerting.Text = "Cập nhật User thất bại, bạn vui lòng thử lại!";
            }
        LoadGrid();

    }
    private void LoadGrid()
    {
        DTP_ACCOUNT objAccount = new DTP_ACCOUNT();
        objAccount.USERNAME = txtUserName.Text.Trim();
        objAccount.FIRSTNAME = txtFirstName.Text.Trim();
        objAccount.LASTNAME = txtLastName.Text.Trim();
        objAccount.PHONE = txtPhone.Text.Trim();
        objAccount.EMAIL = txtEmail.Text.Trim();
        objAccount.MSBOPHAN = int.Parse(ddlMSBOPHAN.SelectedValue);
        objAccount.CSID = int.Parse(ddlCS.SelectedValue);
        objAccount.ACTIVE = chkActive.Checked;
        DisplayUsersInGrid(objAccount);
    }


    protected void grdUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadGrid();
        grdUserList.PageIndex = e.NewPageIndex;
        grdUserList.DataBind();
    }
    private bool CheckEmail(string strEmail)
    {
        string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
        Match match = Regex.Match(strEmail.Trim(), pattern, RegexOptions.IgnoreCase);

        if (match.Success)
            return true;
        else
            return false;
    }
}